<template>
  <div class="heatmap-container">
    <div ref="heatMap" style="width: 100%; height: 300px;"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'LeaveHeatMap',
  mounted() {
    this.initChart()
  },
  methods: {
    initChart() {
      const chart = echarts.init(this.$refs.heatMap)

      const hours = []
      for (let i = 0; i < 24; i++) {
        hours.push(i + '时')
      }

      const days = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']

      const data = []
      days.forEach((day, i) => {
        hours.forEach((hour, j) => {
          // 模拟数据 - 周一周五和上午9-11点是请假高峰
          let value = Math.random() * 10
          if (i === 0 || i === 4) value += 15 // 周一周五
          if (j >= 8 && j <= 11) value += 10 // 上午9-12点
          data.push([i, j, value.toFixed(1)])
        })
      })

      const option = {
        tooltip: {
          position: 'top',
          formatter: function(params) {
            return `${days[params.value[0]]} ${hours[params.value[1]]}<br>请假人数: ${params.value[2]}`
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: hours,
          splitArea: {
            show: true
          },
          axisLabel: {
            interval: 3
          }
        },
        yAxis: {
          type: 'category',
          data: days,
          splitArea: {
            show: true
          }
        },
        visualMap: {
          min: 0,
          max: 30,
          calculable: true,
          orient: 'horizontal',
          left: 'center',
          bottom: '0%',
          inRange: {
            color: ['#e0f3f8', '#abd9e9', '#74add1', '#4575b4', '#313695']
          }
        },
        series: [
          {
            name: '请假人数',
            type: 'heatmap',
            data: data,
            label: {
              show: false
            },
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }

      chart.setOption(option)

      window.addEventListener('resize', function() {
        chart.resize()
      })
    }
  }
}
</script>

<style scoped>
.heatmap-container {
  width: 100%;
}
</style>